package com.zhonghui.wms.service.impl;

import com.zhonghui.common.utils.StringUtils;
import com.zhonghui.wms.domain.WmsVehicleScheduling;
import com.zhonghui.wms.domain.WmsVehicleSchedulingDetails;
import com.zhonghui.wms.domain.vo.WmsVehicleSchedulingVo;
import com.zhonghui.wms.mapper.WmsVehicleSchedulingMapper;
import com.zhonghui.wms.service.IWmsVehicleSchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 智能仓储WMS-运输管理-车辆调度Service业务层处理
 *
 * @author zhonghui
 * @date 2022-05-21
 */
@Service
public class WmsVehicleSchedulingServiceImpl implements IWmsVehicleSchedulingService
{
    @Autowired
    private WmsVehicleSchedulingMapper wmsVehicleSchedulingMapper;

    /**
     * 查询智能仓储WMS-运输管理-车辆调度
     *
     * @param id 智能仓储WMS-运输管理-车辆调度主键
     * @return 智能仓储WMS-运输管理-车辆调度
     */
    @Override
    public WmsVehicleSchedulingVo selectWmsVehicleSchedulingById(Long id)
    {
        return wmsVehicleSchedulingMapper.selectWmsVehicleSchedulingById(id);
    }

    /**
     * 查询智能仓储WMS-运输管理-车辆调度列表
     *
     * @param wmsVehicleSchedulingVo 智能仓储WMS-运输管理-车辆调度
     * @return 智能仓储WMS-运输管理-车辆调度
     */
    @Override
    public List<WmsVehicleSchedulingVo> selectWmsVehicleSchedulingList(WmsVehicleSchedulingVo wmsVehicleSchedulingVo)
    {
        return wmsVehicleSchedulingMapper.selectWmsVehicleSchedulingList(wmsVehicleSchedulingVo);
    }

    /**
     * 新增智能仓储WMS-运输管理-车辆调度
     *
     * @param wmsVehicleScheduling 智能仓储WMS-运输管理-车辆调度
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsVehicleScheduling(WmsVehicleScheduling wmsVehicleScheduling)
    {
        String documentNo = generateNumber("DH");
        wmsVehicleScheduling.setDocumentNo(documentNo);
        int rows = wmsVehicleSchedulingMapper.insertWmsVehicleScheduling(wmsVehicleScheduling);
        insertWmsVehicleSchedulingDetails(wmsVehicleScheduling);
        return rows;
    }

    private String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }
    /**
     * 修改智能仓储WMS-运输管理-车辆调度
     *
     * @param wmsVehicleScheduling 智能仓储WMS-运输管理-车辆调度
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsVehicleScheduling(WmsVehicleScheduling wmsVehicleScheduling)
    {
        wmsVehicleSchedulingMapper.deleteWmsVehicleSchedulingDetailsByVehicleSchedulingId(wmsVehicleScheduling.getId());
        insertWmsVehicleSchedulingDetails(wmsVehicleScheduling);
        return wmsVehicleSchedulingMapper.updateWmsVehicleScheduling(wmsVehicleScheduling);
    }

    /**
     * 批量删除智能仓储WMS-运输管理-车辆调度
     *
     * @param ids 需要删除的智能仓储WMS-运输管理-车辆调度主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsVehicleSchedulingByIds(Long[] ids)
    {
        wmsVehicleSchedulingMapper.deleteWmsVehicleSchedulingDetailsByVehicleSchedulingIds(ids);
        return wmsVehicleSchedulingMapper.deleteWmsVehicleSchedulingByIds(ids);
    }

    /**
     * 删除智能仓储WMS-运输管理-车辆调度信息
     *
     * @param id 智能仓储WMS-运输管理-车辆调度主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsVehicleSchedulingById(Long id)
    {
        wmsVehicleSchedulingMapper.deleteWmsVehicleSchedulingDetailsByVehicleSchedulingId(id);
        return wmsVehicleSchedulingMapper.deleteWmsVehicleSchedulingById(id);
    }

    /**
     * 新增智能仓储WMS-运输管理-车辆调度明细信息
     *
     * @param wmsVehicleScheduling 智能仓储WMS-运输管理-车辆调度对象
     */
    public void insertWmsVehicleSchedulingDetails(WmsVehicleScheduling wmsVehicleScheduling)
    {
        List<WmsVehicleSchedulingDetails> wmsVehicleSchedulingDetailsList = wmsVehicleScheduling.getWmsVehicleSchedulingDetailsList();
        Long id = wmsVehicleScheduling.getId();
        if (StringUtils.isNotNull(wmsVehicleSchedulingDetailsList))
        {
            List<WmsVehicleSchedulingDetails> list = new ArrayList<WmsVehicleSchedulingDetails>();
            for (WmsVehicleSchedulingDetails wmsVehicleSchedulingDetails : wmsVehicleSchedulingDetailsList)
            {
                wmsVehicleSchedulingDetails.setVehicleSchedulingId(id);
                list.add(wmsVehicleSchedulingDetails);
            }
            if (list.size() > 0)
            {
                wmsVehicleSchedulingMapper.batchWmsVehicleSchedulingDetails(list);
            }
        }
    }
}
